import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';
import '../../resources/kq_images.dart';

/// 通用空布局
/// @author 周卓
class KqEmptyView extends StatelessWidget {
  /// 图片路径
  final String? imagePath;

  /// 文字
  final String? text;

  /// 文字颜色
  final Color textColor;

  /// 自定义底部控件
  final Widget? customWidget;

  /// 是否自动根据当前布局是否可滚动，自动撑满剩余空间。
  final bool autoSliver;

  /// 是否强制自动撑满剩余空间。
  final bool forceSliver;

  /// 滚动物理特性
  final ScrollPhysics? physics;

  /// 当[autoSliver]=true时，是否具有滚动主体，一般不用设置
  final bool hasScrollBody;

  /// 当[autoSliver]=true时，是否可填充滚动区域，一般不用设置
  final bool fillOverscroll;

  /// 空布局背景色
  final Color? backgroundColor;

  /// 对齐方式
  final Alignment? alignment;

  /// 外边距
  final EdgeInsets? margin;

  const KqEmptyView(
      {Key? key,
      this.imagePath,
      this.text,
      this.autoSliver = true,
      this.forceSliver = false,
      this.physics,
      this.hasScrollBody = true,
      this.fillOverscroll = false,
      this.alignment,
      this.margin,
      this.textColor = KqThemeColors.textBlack45,
      this.customWidget,
      this.backgroundColor})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (forceSliver ||
        (autoSliver && tryGetContextScrollController(context) != null)) {
      return CustomScrollView(
        physics: physics,
        slivers: [
          SliverFillRemaining(
            hasScrollBody: hasScrollBody,
            fillOverscroll: fillOverscroll,
            child: _buildEmptyView(),
          )
        ],
      );
    } else {
      return _buildEmptyView();
    }
  }

  /// 判断外层是否能滚动
  tryGetContextScrollController(BuildContext context) {
    try {
      return PrimaryScrollController.of(context);
    } catch (e) {
      return null;
    }
  }

  /// 空布局
  _buildEmptyView() {
    return Container(
      color: backgroundColor,
      alignment: alignment ?? Alignment.center,
      margin: margin,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Stack(
            children: [
              Image.asset(
                imagePath ?? Images.commonIcNoData,
                width: 246.r,
                height: 246.r,
                fit: BoxFit.fill,
                package: imagePath != null ? null : KqGlobal.packageName,
              ),
              Positioned(
                  left: 0,
                  right: 0,
                  top: 200.r,
                  child: Text(
                    text ?? KqIntl.currentResource.noDataTip,
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 24.sp, color: textColor),
                  ))
            ],
          ),
          if (customWidget != null) customWidget!,
        ],
      ),
    );
  }
}
